package com.project;


import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.project.frame.common.utils.IdUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * desc
 *
 * @author lw007
 * @date 2021/12/24
 */
public class GeneratorCodeConfig {
    public static void main(String[] args) {
        System.out.println("gen...");
        genCode("qf","clue_area_grid",10);
        genCode("qf","clue_area_grid_0805",10);
    }
    static void genCode(String modelName,String tableName,int sortOrder){
        String userDir = System.getProperty("user.dir");
        String outputDir=userDir+"/gen";
        String author="cjq";
        String dbUrl="jdbc:mysql://10.201.16.32:3306/qfqz?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC&allowPublicKeyRetrieval=true";
        String dbUsername="root";
        String dbPassword="1qaz@WSX";
        String pcParent="com.demo.frame";

        //列名的NamingStrategy
        NamingStrategy namingStrategy = NamingStrategy.underline_to_camel;
//        NamingStrategy namingStrategy = NamingStrategy.no_change;
        genCode(outputDir,author,
                dbUrl,dbUsername,
                dbPassword,modelName,
                pcParent,tableName,
                sortOrder,namingStrategy);
    }

    static void genCode(String outputDir,String author,
                        String dbUrl,String dbUsername,
                        String dbPassword,String modelName,
                        String pcParent,String tableName,
                        int sortOrder,NamingStrategy namingStrategy){
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        //D:\workspace\base_frame_v20210323
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(outputDir);
        gc.setAuthor(author);
        gc.setOpen(false);
        gc.setActiveRecord(true);
        //实体属性 Swagger2 注解
        gc.setSwagger2(true);
//        gc.setEntityName("%sPo");
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setFileOverride(true);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(dbUrl);
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername(dbUsername);
        dsc.setPassword(dbPassword);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(pcParent);
        pc.setController(modelName+".controller");
        pc.setEntity(modelName+".model");
        pc.setMapper(modelName+".dao");
        pc.setXml(modelName+".mapper");
        pc.setService(modelName+".service");
        pc.setServiceImpl(modelName+".service."+"impl");
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
                Map<String, Object> map = new HashMap<>();
                map.put("parent", pc.getParent());
                map.put("modelName",modelName);
                map.put("uuid", IdUtils.id());
                map.put("sortOrder",sortOrder);
                this.setMap(map);
            }
        };

        // 自定义 文件生成
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/codetemplate/web/list_jsp.ftl") {
            //生成html
            @Override
            public String outputFile(TableInfo tableInfo) {
                // TODO Auto-generated method stub
                return outputDir+"/web/"+modelName+"/" + tableInfo.getEntityPath() + "List.jsp";
            }
        });
        focList.add(new FileOutConfig("/codetemplate/web/form_jsp.ftl") {
            //生成html
            @Override
            public String outputFile(TableInfo tableInfo) {
                // TODO Auto-generated method stub
                return outputDir+"/web/"+modelName+"/" + tableInfo.getEntityPath() + "Form.jsp";
            }
        });
        focList.add(new FileOutConfig("/codetemplate/web/show_jsp.ftl") {
            //生成html
            @Override
            public String outputFile(TableInfo tableInfo) {
                // TODO Auto-generated method stub
                return outputDir+"/web/"+modelName+"/" + tableInfo.getEntityPath() + "Show.jsp";
            }
        });
        /*focList.add(new FileOutConfig("/codetemplate/web/menu_sql.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // TODO Auto-generated method stub
                return outputDir+"/web/"+modelName+"/" + tableInfo.getEntityPath() + ".sql";
            }
        });*/
        focList.add(new FileOutConfig("/codetemplate/web/search_html.ftl") {
            //生成html
            @Override
            public String outputFile(TableInfo tableInfo) {
                // TODO Auto-generated method stub
                return outputDir+"/tpl/"+modelName+"/" + tableInfo.getEntityPath() + "Search.html";
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();

        // 配置自定义输出模板
        //指定自定义模板路径，注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别

        templateConfig.setController("/codetemplate/java/controller_rest");
        templateConfig.setService("/codetemplate/java/service");
        templateConfig.setServiceImpl("/codetemplate/java/service_impl");
        templateConfig.setMapper("/codetemplate/java/dao");
        templateConfig.setEntity("/codetemplate/java/entity");
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(namingStrategy);
        strategy.setSuperEntityClass(pc.getParent()+".common.model.BaseModel");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(false);
        // 公共父类
        strategy.setSuperControllerClass(pc.getParent()+".controller.BaseController");
        // 写于父类中的公共字段
        strategy.setInclude(tableName);
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setEntityTableFieldAnnotationEnable(true);
        strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }


}
